// PolicyCompute_v2

export default function (cellData, step = 4) {
  for (let i = 0; i < cellData.length; i++) {
    let cell = cellData[i];
    let sectorRequired = 1;
    let sectorReason = "最低扇区数量";
    let matchedTags = [];
    let inboundValue = 0;
    for (let j = 0; j < computingOptions.length; j++) {
      let option = computingOptions[j];
      let valueCumulated = 0;
      let valueCumulatedArr = 0;
      let valueCumulatedDep = 0;
      let valueCumulatedMisc = 0;
      for (let airport in cell.airport) {
        if (option.airport.includes(airport)) {
          for (let type in cell.airport[airport].type) {
            if (option.type.includes(type)) {
              let total = cell.airport[airport].type[type].total;
              if (type == "ARR") {
                valueCumulatedArr += total;
              } else if (type == "DEP") {
                valueCumulatedDep += total;
              } else {
                valueCumulatedMisc += total;
              }
            }
          }
        }
      }

      // fix step
      valueCumulatedDep = valueCumulatedDep * (4 / step);
      valueCumulatedArr = valueCumulatedArr * (4 / step);

      // test for new params
      // 增加离场负荷
      // 削减离场负荷
      // 削减离场负荷
      // if (valueCumulatedDep > intenseDepThreshold) {
      //   valueCumulatedDep = valueCumulatedDep * intenseMultipler;
      // } else if (valueCumulatedDep < ignoreThresholdDep) {
      //   valueCumulatedDep = 0;
      // } else if (valueCumulatedArr < releaseThresholdArr) {
      //   valueCumulatedDep = valueCumulatedDep * releaseMultiplerDep;
      // }

      // test for new params
      // 进离港权重计算
      valueCumulatedDep = valueCumulatedDep * basicMultiplerDep;
      valueCumulatedArr = valueCumulatedArr * basicMultiplerArr;

      // test for traditional params
      // 进离港合计
      valueCumulated =
        valueCumulatedDep + valueCumulatedArr + valueCumulatedMisc;

      // test for new params
      // 扇区数分段计算
      if (valueCumulated < optThresholdBottom) {
        valueCumulated =
          optMultiplerDepBottom * valueCumulatedDep +
          optMultiplerArrBottom * valueCumulatedArr +
          valueCumulatedMisc;
      } else if (valueCumulated > optThresholdTop) {
        valueCumulated =
          optMultiplerDepTop * valueCumulatedDep +
          optMultiplerArrTop * valueCumulatedArr +
          valueCumulatedMisc;
      } else {
        //
      }

      for (let k = 0; k < option.threshold.length; k++) {
        let threshold = option.threshold[k];
        let tagsValid = true;
        if (threshold.requiredTags && threshold.requiredTags.length > 0) {
          // match Tags
          for (let t = 0; t < threshold.requiredTags.length; t++) {
            if (!matchedTags.includes(threshold.requiredTags[t])) {
              tagsValid = false;
              break;
            }
          }
        }
        if (
          tagsValid &&
          valueCumulated >= threshold.valueCumulated &&
          sectorRequired >= threshold.sectorAlready &&
          sectorRequired < threshold.sectorRequired
        ) {
          sectorRequired = threshold.sectorRequired;
          sectorReason = option.description;
          inboundValue = valueCumulated;
          matchedTags.push(option.tag);
          // console.log(cell.moment, option.description, sectorRequired);
        }
      }
    }
    cellData[i]["sectorRequire"] = sectorRequired;
    cellData[i]["sectorReason"] = sectorReason;
    cellData[i]["inboundValue"] = (inboundValue / 4) * step;
  }
  return cellData;
}

// new params

// 削减离场负荷
const ignoreThresholdDep = 10;
const releaseThresholdArr = 30;
const releaseMultiplerDep = 0.8;

// 增加离场负荷
const intenseDepThreshold = 70;
const intenseMultipler = 1.3;

// 进离港权重计算
const basicMultiplerArr = 1.5;
const basicMultiplerDep = 0.5;

// 扇区数分段计算 TOP
const optThresholdTop = 90;
const optMultiplerArrTop = 0.9;
const optMultiplerDepTop = 0.9;

// 扇区数分段计算 BOTTOM
const optThresholdBottom = 90;
const optMultiplerDepBottom = 1;
const optMultiplerArrBottom = 1;

// traditional params
const computingOptions = [
  {
    // "总量 > 116@8扇; 105@7扇; 85@6扇; 75@5扇; 65@4扇; 45@3扇; 21@2扇"
    description: "125=8扇; 110=7扇; 90=6扇; 80=5扇; 65=4扇; 45=3扇; 21=2扇",
    tag: "total_general_compute",
    airport: ["ZUUU", "ZUTF", "ZUMY"],
    type: ["ARR", "DEP"],
    threshold: [
      {
        valueCumulated: 125,
        sectorAlready: 0,
        sectorRequired: 8,
        requiredTags: [],
      },
      {
        valueCumulated: 110,
        sectorAlready: 0,
        sectorRequired: 7,
        requiredTags: [],
      },
      {
        valueCumulated: 90,
        sectorAlready: 0,
        sectorRequired: 6,
        requiredTags: [],
      },
      {
        valueCumulated: 80,
        sectorAlready: 0,
        sectorRequired: 5,
        requiredTags: [],
      },
      {
        valueCumulated: 65,
        sectorAlready: 0,
        sectorRequired: 4,
        requiredTags: [],
      },
      {
        valueCumulated: 45,
        sectorAlready: 0,
        sectorRequired: 3,
        requiredTags: [],
      },
      {
        valueCumulated: 21,
        sectorAlready: 0,
        sectorRequired: 2,
        requiredTags: [],
      },
      {
        valueCumulated: 0,
        sectorAlready: 0,
        sectorRequired: 1,
        requiredTags: [],
      },
    ],
  },
  // {
  //   description: "总量@3扇; ZUUU进港?20架次; @4扇",
  //   tag: "ZUUU_ARR_20",
  //   airport: ["ZUUU"],
  //   type: ["ARR"],
  //   threshold: [
  //     {
  //       valueCumulated: 21,
  //       sectorAlready: 3,
  //       sectorRequired: 4,
  //       requiredTags: [],
  //     },
  //   ],
  // },
  // {
  //   description: "总量@3扇; ZUTF进港>20架次; @4扇",
  //   tag: "ZUTF_ARR_20",
  //   airport: ["ZUTF"],
  //   type: ["ARR"],
  //   threshold: [
  //     {
  //       valueCumulated: 21,
  //       sectorAlready: 3,
  //       sectorRequired: 4,
  //       requiredTags: [],
  //     },
  //   ],
  // },
  // {
  //   description: "ZUUU进港架次≥19; @3扇",
  //   tag: "ZUUU_ARR_19",
  //   airport: ["ZUUU"],
  //   type: ["ARR"],
  //   threshold: [
  //     {
  //       valueCumulated: 19,
  //       sectorAlready: 0,
  //       sectorRequired: 3,
  //       requiredTags: [],
  //     },
  //   ],
  // },
  // {
  //   description: "ZUTF进港架次≥19; @3扇",
  //   tag: "ZUTF_ARR_19",
  //   airport: ["ZUTF"],
  //   type: ["ARR"],
  //   threshold: [
  //     {
  //       valueCumulated: 19,
  //       sectorAlready: 0,
  //       sectorRequired: 3,
  //       requiredTags: [],
  //     },
  //   ],
  // },
  // {
  //   description: "ZUUU/ZUTF进港架次均≥19; @5扇",
  //   tag: "ZUUU&ZUTF_ARR_19",
  //   airport: ["ZUUU", "ZUTF"],
  //   type: ["ARR"],
  //   threshold: [
  //     {
  //       valueCumulated: 38,
  //       sectorAlready: 0,
  //       sectorRequired: 5,
  //       requiredTags: ["ZUUU_ARR_19", "ZUTF_ARR_19"],
  //     },
  //   ],
  // },
];
